"use strict";
cc._RF.push(module, '7f8c4Ifl4tPAKIGfkOKH7/z', 'RechargeUI');
// scripts/mainScene/RechargeUI.js

"use strict";

/**
 * 主界面
 * 充值界面
*/
cc.Class({
    extends: cc.Component,

    properties: {
        labName: cc.Label, //账户名
        labBalance: cc.Label, //余额
        labMoney: cc.Label, //充值余额
        curSelToggle: cc.Toggle,
        ndSpSelBg: cc.Node,
        rechargePop: cc.Prefab, //选择支付宝方式支付，弹出的界面
        fbWechatPop: cc.Prefab, //微信支付
        _payWay: null, //支付的方式
        _payMoney: "20",
        _showMing: false
    },

    // use this for initialization
    onLoad: function onLoad() {
        this.labName.string = User.getNickName();
        this.labBalance.string = User.getBalance().toFixed(2) + "元宝";
        this.labMoney.string = "20";
        this._payMoney = "20";
        this._money = "";
        this._payWay = '1'; //默认选中微信支付

        window.Notification.on("moneyShow", function (data) {
            this.moneyShow();
        }, this);

        window.Notification.on("updateRechargeUI", function (data) {
            this.updateUI();
        }, this);
    },

    updateUI: function updateUI() {
        this.labName.string = User.getNickName();
        this.labBalance.string = User.getBalance().toFixed(2) + "元宝";
    },

    //用户余额
    moneyShow: function moneyShow() {
        if (this._showMing) {
            return;
        }
        this.labBalance.string = '';
        this._showMing = true;
        var recv = function (ret) {
            ComponentsUtils.unblock();
            this._showMing = false;
            if (ret.Code === 0) {
                User.setBalance(ret.Balance);
                User.setGoldBean(ret.Gold);
                cc.log('rechargeui 当前用户余额', User.getBalance());
            } else {
                ComponentsUtils.showTips(ret.Msg);
            }
            this.labBalance.string = User.getBalance().toFixed(2) + '元宝';
        }.bind(this);
        var data = {
            Token: User.getLoginToken(),
            UserCode: User.getUserCode()
        };
        CL.HTTP.sendRequest(DEFINE.HTTP_MESSAGE.GETUSERMONEY, data, recv.bind(this), "POST");
        ComponentsUtils.block();
    },

    //选择要充值的金额 
    onSelectMoneyBtn: function onSelectMoneyBtn(toggle, customEventData) {
        this.labMoney.string = customEventData;
        this.curSelToggle = toggle;
        this.ndSpSelBg.active = false;
        this._payMoney = customEventData;
    },

    //输入框中调用的方法
    onDidBegan: function onDidBegan() {
        this.ndSpSelBg.active = true;
        this.curSelToggle.getComponent(cc.Toggle).isChecked = false;
    },

    onDidRetMoney: function onDidRetMoney(editbox, customEventData) {
        if (!Utils.isInt(editbox.string)) {
            editbox.string = this._payMoney;
            return;
        }
        var num = parseInt(editbox.string);
        this._payMoney = num.toString();
        this.labMoney.string = num.toString();
    },

    //支付方式(微信支付 QQ钱包支付 支付宝支付)
    onTgPayWay: function onTgPayWay(toggle, customEventData) {
        this._payWay = customEventData;
    },

    onPay: function onPay() {
        if (!User.getIsvermify()) {
            ComponentsUtils.showTips("请先登录!");
            return;
        }

        if (!Utils.isInt(this.labMoney.string)) {
            ComponentsUtils.showTips("充值金额格式不对！");
            return;
        }

        if (this.labMoney.string === "" || parseInt(this.labMoney.string) <= 0) {
            ComponentsUtils.showTips("充值金额不能为空！");
            return;
        }
        var payMoney = parseInt(this.labMoney.string);
        if (payMoney > 300000) {
            ComponentsUtils.showTips("一次性充值最大只能300000元宝！");
            return;
        } else if (payMoney < 5) {
            ComponentsUtils.showTips("充值金额最低5元起!");
            return;
        }

        switch (this._payWay) {
            case '1':
                //微信支付
                {
                    var self = this;
                    var recv = function (ret) {
                        ComponentsUtils.unblock();
                        if (ret.Code !== 0) {
                            cc.error(ret.Msg);
                            ComponentsUtils.showTips(ret.Msg);
                        } else {
                            //cc.log('扫码',ret.Data);
                            var canvas = cc.find("Canvas");
                            var wechatPop = cc.instantiate(this.fbWechatPop);
                            var url = ret.Data.PayLink;
                            wechatPop.getComponent(wechatPop.name).init(url, this._payMoney, ret.Data.OrderNo);
                            canvas.addChild(wechatPop);
                        }
                    }.bind(this);

                    var data = {
                        Token: User.getLoginToken(),
                        UserCode: User.getUserCode(),
                        Amount: LotteryUtils.moneytoClient(this._payMoney),
                        TbBody: "wxpay",
                        TbAttach: "wxpay"
                    };
                    CL.HTTP.sendRequestRet(DEFINE.HTTP_MESSAGE.WXPAY, data, recv.bind(this), "POST");
                    ComponentsUtils.block();
                }
                break;
            case '2':
                //QQ支付
                {
                    ComponentsUtils.showTips('暂时不支持QQ支付');
                }
                break;
            case '3':
                //支付宝支付
                {
                    var self = this;
                    var recv = function (ret) {
                        ComponentsUtils.unblock();
                        if (ret.Code !== 0) {
                            cc.error(ret.Msg);
                            ComponentsUtils.showTips(ret.Msg);
                        } else {
                            //cc.log('支付',ret.Data);
                            var canvas = cc.find("Canvas");
                            var rechargePop = cc.instantiate(this.rechargePop);
                            var url = ret.Data.PayLink;
                            rechargePop.getComponent(rechargePop.name).init(url);
                            canvas.addChild(rechargePop);
                        }
                    }.bind(this);

                    var data = {
                        Token: User.getLoginToken(),
                        UserCode: User.getUserCode(),
                        Amount: LotteryUtils.moneytoClient(this._payMoney),
                        TbBody: "alipay",
                        TbAttach: "alipay"
                    };
                    CL.HTTP.sendRequestRet(DEFINE.HTTP_MESSAGE.ALIPAY, data, recv.bind(this), "POST");
                    ComponentsUtils.block();
                }
                break;
        }
    },

    onDestroy: function onDestroy() {
        window.Notification.offType("moneyShow");
    }

});

cc._RF.pop();